package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-13 15:20
 */
public class 最大连续1的个数_485 {
    public int findMaxConsecutiveOnes(int[] nums) {
        int total = 0;
        int current = 0;
        for (int num : nums) {
            // 数组中数值为 1
            if (num == 1) {
                current++;
            } else {
                if (current > total) {
                    total = current;
                }
                current = 0;
            }
        }
        return Math.max(current, total);
    }

    public int findMaxConsecutiveOnesOld(int[] nums) {
        int total = 0;
        int temp = 0;
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            // 数组中数值为 1
            if (nums[i] == 1) {
                // 第一次是 1
                if (index + 1 != i) {
                    index = i;
                    temp = 1;
                // 后面连续是 1
                } else if (index + 1 == i) {
                    index = i;
                    temp++;
                }
                // 最后一次循环
                if(i == nums.length - 1) {
                    if(temp > total) {
                        total = temp;
                    }
                }
            } else {
                if(temp > total) {
                    total = temp;
                }
                temp = 0;
            }
        }
        return total;
    }

    @Test
    public void solution() {
        int[] ints = new int[]{1, 1, 1, 4, 5, 1, 1, 1, 1};
        int maxConsecutiveOnes = findMaxConsecutiveOnes(ints);
        System.out.println(maxConsecutiveOnes);
    }
}
